package com.example.highclasscomponent;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.highclasscomponent.adapter.PlanetBaseAdapter;
import com.example.highclasscomponent.pojo.Planet;

import java.util.List;

public class BaseAdapterActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    private List<Planet> mPlanetList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_adapter);
        Spinner spn = findViewById(R.id.spn);
        mPlanetList = Planet.getDefaultList();
        //构建适配器
        PlanetBaseAdapter adapter=new PlanetBaseAdapter(this, mPlanetList);
        spn.setAdapter(adapter);
        spn.setSelection(0);
        spn.setOnItemSelectedListener(this);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this, mPlanetList.get(position).toString(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    }
}
